"""
Problem 39: https://projecteuler.net/problem=39

If p is the perimeter of a right angle triangle with integral length sides,
{a,b,c}, there are exactly three solutions for p = 120.
{20,48,52}, {24,45,51}, {30,40,50}

For which value of p ≤ 1000, is the number of solutions maximised?
"""

# _*_ conding:UTF-8 _*_
'''
@author = Kuperain
@email = kuperain@aliyun.com
@IDE = VSCODE Python3.8.3
@creat_time = 2022/5/13                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     
'''


SquareNumbers = [i*i for i in range(0, 1000)]


def answers(p: int = 120) -> int:
    '''
        1. a^2 + b^2 = c^2
        2. p = a + b + c
        3. a <= b < c  # assume a<=b
        4. a + b > c

    solve max(len({(a,b) with:
                              (1) a^2 + b^2 = (p-a-b)^2
                              (2) 1 <= a <= b <= (p-a-b)-1   <==> 1 <= a <= b <= (p-a-1)/2
                              (3) a+b >= (p-a-b) + 1         <==> a+b >= (p+1)/2
                  }))


    >>> print(answers())
    3
    '''
    global SquareNumbers

    count = 0
    for a in range(1, (p-1)//3 + 1):
        for b in range(max(a, (p-1)//2 - a), (p-a-1)//2 + 1):
            if SquareNumbers[a] + SquareNumbers[b] == SquareNumbers[p-a-b]:
                # print((a,b,p-a-b),end='')
                count += 1
    # if count>0: print()
    return count


def solution(limit: int = 1000) -> int:
    res = 0
    maxP = 3
    for p in range(3, limit+1):
        tmp = answers(p)

        if tmp > res:
            res = tmp
            maxP = p
            print(p, tmp)

    return maxP, res


if __name__ == "__main__":
    import doctest
    doctest.testmod(verbose=False)

    print(solution())
    # (840, 8)

    '''
    12 1
    60 2
    120 3
    240 4
    420 5
    720 6
    840 8
    '''
